pub mod users;

use std::env::current_dir;
use rusqlite::{Connection};
use anyhow::Result;

/// 数据库操作对象
pub struct Db {
    conn: Connection,
}

impl Db {
    /// 初始化数据库
    pub fn init() -> Result<Db> {
        let db_path = current_dir().unwrap().join("db.sqlite");
        let conn = Connection::open(&db_path)?;
        let db = Db { conn };
        db.init_tables();
        Ok(db)
    }
    /// 初始化数据表
    fn init_tables(&self) {
        self.init_users();
    }
    /// 查看某个表是否存在
    fn table_exists(&self, table_name: &str) -> bool {
        let sql = format!("pragma table_info({table_name})");
        let mut stmt = self.conn.prepare(&sql).unwrap();
        let mut rows = stmt.query([]).unwrap();
        let result = rows.next().unwrap();
        result.is_some()
    }
}